//
//  ListTable.m
//  CWeekly
//
//  Created by  on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ListTable.h"
#import "Page.h"
@implementation ListTable
@synthesize table;
@synthesize dataArray;
@synthesize  delegate;
@synthesize interfaceOrientation;
@synthesize  imId;
- (void)dealloc{
    [imId release];
    [dataArray release];
    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        table=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        table.delegate=self;
        table.dataSource=self;
        table.backgroundColor=[UIColor clearColor];
        [self addSubview:table];
        [table release];
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Forum* form=(Forum*)[dataArray objectAtIndex:section];
    NSLog(@"section=%d,list row===%d",section,form.articles.count);

    return form.articles.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    Forum* form=(Forum*)[dataArray objectAtIndex:section];
    return form.title;
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* iden=@"cell";
    
    UITableViewCell* cell=[table dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
    }
    
    Forum* form=(Forum*)[dataArray objectAtIndex:indexPath.section];
    Article* art= (Article*)[form.articles objectAtIndex:indexPath.row];
    Page*pg=(Page*)[art.pages objectAtIndex:0];
    NSString* imgName=pg.v_thumbnails;
    cell.imageView.image=[self img:imgName forOrientation:interfaceOrientation];
    cell.textLabel.text=art.title;
    cell.textLabel.textColor=[UIColor whiteColor];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Forum* form=(Forum*)[dataArray objectAtIndex:indexPath.section];
    Article* art= (Article*)[form.articles objectAtIndex:indexPath.row];
    Page*pg=(Page*)[art.pages objectAtIndex:0];
    [delegate listTableSelectedPage:pg];

}
-(UIImage*)img:(NSString*)imgName forOrientation:(UIInterfaceOrientation)orientation{
//    NSString* directory=nil;
//    if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft||interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
//        directory=[NSString stringWithFormat:@"Documents/0423"];
//    }else {
//        directory=[NSString stringWithFormat:@"Documents/0423"];
//    }
//    NSArray* a=[imgName componentsSeparatedByString:@"."];
    NSString*imgPath=[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BookData/%@/%@",imId,imgName]];
    imgPath=[imgPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    NSLog(@"%@",imgPath);
    NSData* da=[NSData dataWithContentsOfFile:imgPath];
    NSLog(@"%d",da.length);
    UIImage*im=[UIImage imageWithData:da];
    return im;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIImage* im=[UIImage imageNamed:@"advs_bg.png"];
    [im drawInRect:rect];
}


@end
